package com.fulihui.usercore.core.processor.auth;

import com.fulihui.usercore.common.util.Errors;
import com.fulihui.usercore.dal.dataobj.LoginDO;
import com.fulihui.usercore.dto.UserDTO;
import com.fulihui.usercore.enums.BooleanValueEnum;
import com.fulihui.usercore.enums.UserGenderEnum;
import com.fulihui.usercore.enums.UserRegisterTypeEnum;
import com.fulihui.usercore.enums.UserStateEnum;
import com.fulihui.usercore.request.auth.AbstractAuthRequest;
import com.fulihui.usercore.request.auth.QuickAuthRequest;
import com.fulihui.usercore.sdk.domain.SignTypeEnum;
import org.near.servicesupport.error.InvokeServiceException;
import org.near.toolkit.common.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.singletonList;

/**
 * @author Willard.Hu on 2016/11/9 0009.
 */
@Component("quickLoginAuthProcessor")
public class QuickLoginAuthProcessor extends AbstractAuthProcessor {

    @Override
    public String authType() {
        return UserRegisterTypeEnum.MOBILE_QUICK.getCode();
    }

    @Override
    protected UserDTO toUserModel(AbstractAuthRequest request) {
        UserDTO userDTO = new UserDTO();
        userDTO.setRegisterType(authType());
        userDTO.setRegisterSource(request.getRegisterSource());
        userDTO.setGender(UserGenderEnum.UNKNOW.getCode());
        userDTO.setNickname(nicknameGenerator.generate());

        // 快捷登陆账号就是手机号
        userDTO.setMobileNo(request.getLoginId());
        userDTO.setMobileVerified(BooleanValueEnum.TRUE.isTrue());

        userDTO.setState(UserStateEnum.NOMAL.getCode());
        userDTO.setRoleCode("USER_NOMAL");
        userDTO.setProvince(request.getProvince());
        userDTO.setCity(request.getCity());
        Date now = new Date();
        userDTO.gmtCreate(now);
        userDTO.lastLogin(now);

        if (request.getUsedProduct() != null) {
            userDTO.setUsedProducts(singletonList(request.getUsedProduct()));
        }
        userDTO.setErduoVersion(request.getErduoVersion());

        Map<String, Object> param = new HashMap<>();
        if (StringUtil.isNotBlank(request.getUserIP())) {
            param.put("userIp", request.getUserIP());
        }
        if (param.size() > 0) {
            userDTO.setExtInfo(param);
        }

        return userDTO;
    }

    /**
     * 快捷登陆屏蔽注册方法
     * @param request {@link AbstractAuthRequest}
     * @return userid
     */
    @Override
    public String register(AbstractAuthRequest request) {
        throw new UnsupportedOperationException("QuickLoginAuthProcessor.register");
    }

    /**
     * 快捷登陆，登陆注册一起完成，上层无需调用 register 方法
     * @param request {@link AbstractAuthRequest}
     * @return userid
     */
    @Override
    public String login(AbstractAuthRequest request) {
        QuickAuthRequest quickAuthRequest = (QuickAuthRequest) request;
        if (!checkSmsCode(quickAuthRequest.getLoginId(), quickAuthRequest.getVcode(),
                quickAuthRequest.getVcodeNamespace())) {
            throw new InvokeServiceException(Errors.VCode.VCODE_VALIDATION_FAILS);
        }
        LoginDO loginDO = userLoginRepository.queryByPK(quickAuthRequest.getLoginId());
        String userId;
        if (loginDO != null) {
            //fixme 客户表 不再维护
            //bindingCustomer(request.getLoginId(), loginDO.getUserId());
            userId = loginDO.getUserId();
            //检查用户是否禁用
            checkLoginUserState(userId);
            //登入发布消息
            signPublish(request, userId, SignTypeEnum.SIGN_IN);
            asyncSignInExecute(userId, request);
        } else {
            userId = super.register(request);
        }
        return userId;
    }

}
